﻿using System;
using System.Text;
using System.Text.RegularExpressions;

namespace visinia.SmartEngine
{    
    public class WebHelpers
    {
        public static int Int32ParseSafe(string Value, int defaultValue)
        {
            if (string.IsNullOrEmpty(Value))
                return defaultValue;
            else
            {
                int count = 0;
                if (Int32.TryParse((string)Value, out count))
                {
                    if (count < 1)
                        return 0;
                }
                return count;
            }
        }

        private static readonly Regex Reg_HTML = new Regex("<[^>]*>", RegexOptions.Compiled);        
        public static string StripHtml(string html)
        {
            if (string.IsNullOrEmpty(html))
                return string.Empty;
            return Reg_HTML.Replace(html, string.Empty);
        }        

        /// <summary>
        /// A helper method to create the html lists
        /// </summary>                
        public static string GenerateBulletedListHyperLinks(string[] TextArray, string[] links)
        {
            StringBuilder sb = new StringBuilder();
            string link = "<li><a href=\"{0}\">{1}</a></li>";
            sb.Append("<ul>");
            for (int index = 0; index < TextArray.Length; index++)  
                sb.AppendFormat(link, links[index], TextArray[index]);            
            sb.Append("</ul>");
            return sb.ToString();
        }

        /// <summary>
        /// A helper method to create the html lists
        /// </summary>                
        public static string GenerateBulletedListHyperLinks(string[] TextArray, string[] links, string currentUrl)
        {            
            StringBuilder sb = new StringBuilder();
            string link = "<li><a href=\"{0}\">{1}</a></li>";
            string linkActive = "<li class=\"current\"><a href=\"{0}\">{1}</a></li>";
            sb.Append("<ul>");
            
            string rroot = WebRoots.GetRelativeWebRoot;
            currentUrl = currentUrl.Remove(0, rroot.Length);
            if (currentUrl.EndsWith(".aspx"))
                currentUrl = currentUrl.Remove(currentUrl.Length - 5);

            for (int index = 0; index < TextArray.Length; index++)
            {
                string tempUrl = links[index];    
                if (tempUrl.EndsWith(".aspx"))
                    tempUrl = tempUrl.Remove(tempUrl.Length - 5);
                if (tempUrl.StartsWith(WebRoots.GetWebRoot))
                {
                    string root = WebRoots.GetWebRoot;
                    tempUrl = tempUrl.Remove(0, root.Length);
                }
                if (currentUrl.StartsWith(tempUrl, StringComparison.OrdinalIgnoreCase))
                    sb.AppendFormat(linkActive, links[index], TextArray[index]);
                else
                    sb.AppendFormat(link, links[index], TextArray[index]);
            }
            sb.Append("</ul>");
            return sb.ToString();
        }        
    }
}
